package com.example.vehiclemanagesystem.controller;


import com.example.vehiclemanagesystem.entity.User;
import com.example.vehiclemanagesystem.service.UserService;
import com.fasterxml.jackson.databind.DatabindContext;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class HomeController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Map<String, Object> registerNewUser(@RequestBody Map<String, String> remap){
        String name=remap.get("name");
        String password=remap.get("password");
        Map<String, Object> map=new HashMap<>();
        try{
            User user=new User(name,password);
            User user1=userService.selectUserByUsername(name);
            if(user1!=null){
                map.put("success",false);
                map.put("message","用户名已注册！");
            }else {
                userService.registerNewUser(user);
                map.put("success",true);
                map.put("message","注册成功! ");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("message","注册失败！");
        }
        return map;
    }
    @RequestMapping("/menu")
    public Map<String,Object> menu(HttpServletRequest request){

        Map<String, Object> map = new HashMap<>();
        HttpSession session = request.getSession();
        System.out.println(request.getSession().getId());
        //String name=(String) session.getAttribute("username");
        String name=userService.selectUserByUsername("usernow").getPassword();
        User user = userService.selectUserByUsername(name);
        System.out.println(name);
        map.put("nameNow",name);
        map.put("userId",user.getU_id());
        if(name.equals("admin"))
        {
            List<admin> adminList=new ArrayList<>();
            adminList.add(new admin(1,"查看通知","usermessage"));
            adminList.add(new admin(2,"查看路线","usermap"));
            adminList.add(new admin(3,"留言板","messageBoard"));
            //adminList.add(new admin(4,"增删车辆","cars"));
            //adminList.add(new admin(5,"车辆信息管理",""));
            map.put("success",true);
            map.put("权限参数",adminList);
        }
        else if(name.equals("adjust")){
            List<admin> adminList=new ArrayList<>();
            adminList.add(new admin(3,"留言板","messageBoard"));
            adminList.add(new admin(6,"通知管理","administratormessage"));
            adminList.add(new admin(7,"路线管理","administratormap"));
            adminList.add(new admin(8,"车辆信息管理","cars"));
            adminList.add(new admin(8,"出车信息管理","vehicleDispatchingInformationManagement"));
            map.put("success",true);
            map.put("权限参数",adminList);
        }
        else if(name.equals("user")){
            List<admin> adminList=new ArrayList<>();
            adminList.add(new admin(1,"查看通知","usermessage"));
            adminList.add(new admin(2,"查看路线","usermap"));
            adminList.add(new admin(3,"留言板","messageBoard"));
            adminList.add(new admin(9,"人事管理","people"));
            map.put("success",true);
            map.put("权限参数",adminList);
        }
        else if(name.equals("serve"))
        {
            List<admin> adminList=new ArrayList<>();
            adminList.add(new admin(1,"查看通知","usermessage"));
            adminList.add(new admin(2,"查看路线","usermap"));
            adminList.add(new admin(10,"留言管理","messageBoardManege"));
            map.put("success",true);
            map.put("权限参数",adminList);
        }
        else
        {
            List<admin> adminList=new ArrayList<>();
            map.put("success", true);
            adminList.add(new admin(4,"用户通知","Usermessage"));
            adminList.add(new admin(6,"用户查看路线","Usermap"));
            adminList.add(new admin(6,"留言板","messageBoard"));
            map.put("权限参数",adminList);
        }
        return map;
    }

    @PostMapping("/login")
    public Map<String, Object> loginUser(@RequestBody Map<String, String> remap, HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println("进入登录页面");
        String name = remap.get("name");
        String password = remap.get("password");
        System.out.println(request.getSession().getId());
        Map<String, Object> map = new HashMap<>();
        try {
            if(name.equals("admin")){
                if(password.equals("admin"))
                {
                    request.getSession().setAttribute("username", name);
                    User user1 = userService.selectUserByUsername(name);
                    User usernow=new User("usernow",name);
                    map.put("debug",usernow);
                    userService.setUserNow(usernow);
                    map.put("success",true);
                    map.put("message","管理员登陆成功");
                    //map.put("权限参数",adminList);
                    request.getSession().setAttribute("u_id", user1.getU_id());
                    return map;
                }
                else
                {
                    map.put("message","管理员密码错误");
                    map.put("success",false);
                    return map;
                }
            }
            User user = new User(name, password);
            User user1 = userService.selectUserByUsername(user.getName());
            if (user1 == null) {
                map.put("success", false);
                map.put("message", "用户名输入错误");
            } else {
                if (!password.equals(user1.getPassword())) {
                    map.put("success", false);
                    map.put("message", "密码输入错误");
                } else {
                    request.getSession().setAttribute("username", name);
                    request.getSession().setAttribute("u_id", user1.getU_id());
                    map.put("success", true);
                    map.put("message", "登录成功");
                    User usernow=new User("usernow",name);
                    map.put("debug",usernow);
                    userService.setUserNow(usernow);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "用户登录失败！");
        }
        return map;
    }

}
